const Controller = require('egg').Controller;


class wxAddress extends Controller {

    async default(){
        const {app,ctx,service} = this;
        const {id} = ctx.request.query;
        let param = {
            where:{
                user_id:id,
                is_default:'1',
                deleted:'0'
            }
        }
        let data = await ctx.model.FdAddress.findOne(param);
        ctx.body = {
            code : 200,
            data : data,
            msg : ''
        }
    }

    async all(){
        const {app,ctx,service} = this;
        const {id} = ctx.request.query;
        let param = {
            where : {
                user_id:id,
                deleted:'0'
            }
        }
        let data = await ctx.model.FdAddress.findAll(param);
        ctx.body = {
            code : 200,
            data : data,
            msg : ''
        }
    }

    async del(){
        const {app,ctx,service} = this;
        const {id} = ctx.request.query;
        //开启事务
        let t = await ctx.model.transaction();
        try {
            //调用sequelize update更新为批量更新 只要满足where条件的数据都将被更新
            let rs = await ctx.model.FdAddress.update({deleted:'1'},{where:{id},transaction:t});

            //提交事务
            t.commit();
        }catch (e) {
            //失败回滚
            t.rollback();
            //抛出错误信息
            ctx.throw(500,e.message)
        }

        ctx.body = {
            code : 200,
            data : {},
            msg : ''
        }
    }

    async setDefault(){
        const {app,ctx,service} = this;
        const {id,user_id} = ctx.request.query;
        //开启事务
        let t = await ctx.model.transaction();
        try {
            //调用sequelize update更新为批量更新 只要满足where条件的数据都将被更新
            let rs = await ctx.model.FdAddress.update({is_default:'0'},{where:{user_id},transaction:t});
            rs = await ctx.model.FdAddress.update({is_default:'1'},{where:{id},transaction:t});

            //提交事务
            t.commit();
        }catch (e) {
            //失败回滚
            t.rollback();
            //抛出错误信息
            ctx.throw(500,e.message)
        }

        ctx.body = {
            code : 200,
            data : {},
            msg : ''
        }
    }

    async update(){
        const {app,ctx,service} = this;
        const {address} = ctx.request.body;
        //开启事务
        let t = await ctx.model.transaction();
        try {
            //调用sequelize update更新为批量更新 只要满足where条件的数据都将被更新
            delete address.created_at;
            delete address.updated_at;
            delete address.deleted;
            if(address.is_default == '1'){
                let rs = await ctx.model.FdAddress.update({is_default:'0'},{where:{user_id:address.user_id},transaction:t});
            }
            let rss = await ctx.model.FdAddress.update(address,{where:{id:address.id},transaction:t});

            //提交事务
            t.commit();
        }catch (e) {
            //失败回滚
            t.rollback();
            //抛出错误信息
            ctx.throw(500,e.message)
        }

        ctx.body = {
            code : 200,
            data : {},
            msg : ''
        }
    }

    async add(){
        const {app,ctx,service} = this;
        var uuid = require('node-uuid');
        let {address} = ctx.request.body;
        //开启事务
        let t = await ctx.model.transaction();
        try {
            //调用sequelize update更新为批量更新 只要满足where条件的数据都将被更新
            address = {
                ...address,
                deleted:'0',
                id:uuid.v1()
            }
            if(address.is_default == '1'){
                let rs = await ctx.model.FdAddress.update({is_default:'0'},{where:{user_id:address.user_id},transaction:t});
            }
            let rss = await ctx.model.FdAddress.create(address,{transaction:t});

            //提交事务
            t.commit();
        }catch (e) {
            //失败回滚
            t.rollback();
            //抛出错误信息
            ctx.throw(500,e.message)
        }

        ctx.body = {
            code : 200,
            data : {},
            msg : ''
        }
    }
}

module.exports = wxAddress;
